package com.fengdi.publicObject;

import java.util.ArrayList;

public class ListNode implements Comparable<ListNode>{
    public int val;
    public ListNode next;

    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    public static ListNode createListNode(int[] arr) {
        ListNode head = new ListNode();
        ListNode node = head;
        for (int i = 0; i < arr.length; i++) {
            node.val = arr[i];
            if (i != arr.length - 1) {
                node.next = new ListNode();
                node = node.next;
            }
        }
        return head;
    }

    @Override
    public int compareTo(ListNode o) {
        if(this.val > o.val) {
            return 1;
        }else if(this.val == o.val) {
            return 0;
        }else {
            return -1;
        }
    }

    @Override
    public String toString() {
        ArrayList<Integer> list = new ArrayList<>();
        ListNode node = this;
        while (node != null) {
            list.add(node.val);
            node = node.next;
        }
        return list.toString();
    }
}
